Skip to content
This repository was archived by the owner on Feb 25, 2026. It is now read-only.

Expose public indexer and length properties on RefEnumerable<T> and ReadOnlyRefEnumerable<T> - #4047

Merged
10 commits merged into
CommunityToolkit:mainfrom
DaZombieKiller:feature/refenumerable-indexer
Jul 7, 2021
Merged

10 commits merged into
CommunityToolkit:mainfrom
DaZombieKiller:feature/refenumerable-indexer

Conversation

@DaZombieKiller

@DaZombieKiller DaZombieKiller commented May 26, 2021 •

Copy link
Copy Markdown

Closes #4043

PR Type

What kind of change does this PR introduce?

  • Feature

What is the current behavior?

Currently, RefEnumerable<T> (and its read-only variant) do not expose an indexer or their length. This can make it difficult to implement certain APIs against them, such as a function to reverse their data.

What is the new behavior?

This PR exposes public length and indexer properties (for both int and System.Index) on both RefEnumerable<T> and ReadOnlyRefEnumerable<T>.

public partial readonly ref struct RefEnumerable<T>
{
+   public int Length { get; }
+   public ref T this[int index] { get; }
+   public ref T this[Index index] { get; }
}

public partial readonly ref struct ReadOnlyRefEnumerable<T>
{
+   public int Length { get; }
+   public ref readonly T this[int index] { get; }
+   public ref readonly T this[Index index] { get; }
}

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tested code with current supported SDKs
  • Pull Request has been submitted to the documentation repository instructions. Link:
  • Sample in sample app has been added / updated (for bug fixes / features)
  • New major technical changes in the toolkit have or will be added to the Wiki e.g. build changes, source generators, testing infrastructure, sample creation changes, etc...
  • Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Header has been added to all new source files (run build/UpdateHeaders.bat)
  • Contains NO breaking changes

Other information

It may be worthwhile to additionally expose the Step field and the newly-added DangerousGetReference/DangerousGetReferenceAt methods. This would allow for an implementation of Reverse that avoids bounds-checking.

Loading
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

auto merge ⚡ feature 💡 high-performance 🚂 Issues/PRs for the Microsoft.Toolkit.HighPerformance package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature, HighPerformance] Public length and indexer properties for RefEnumerable<T>

4 participants